package com.ziguang.redwood.admin.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

public class ProductService {
	private static final Logger log = LoggerFactory.getLogger(ProductService.class);

	public static final ProductService me = new ProductService();
	
	/**
	 * 查询产品信息
	 */
	public List<Record> findProduct() {
		List<Record> category = Db.find("SELECT p.*,t.type_name FROM rw_product p LEFT JOIN rw_product_type t "
				+ "ON p.type_id = t.id ORDER BY p.sort");
		return category;
	}
	
	/**
	 * 查询分类信息
	 */
	public List<Record> findCategory(){
		List<Record> list = Db.find("select * from rw_product_type order by sort");
		return list;
	}
	
	/**
	 * 查询当前产品信息
	 */
	public Record QueryEdit(String id) {
		Record data = Db.findFirst("SELECT p.*,t.type_name FROM rw_product p LEFT JOIN rw_product_type t "
				+ "ON p.type_id = t.id WHERE p.id = ? ORDER BY p.sort", id);
		return data;
	}
	
	
	/**
	 * 保存当前产品信息
	 */
	public void save(String id,String productName,String productInfo,String sqlSmallImg,String sqlBigImg,String typeId,String sort){
		if(!id.isEmpty()){
			Db.update("update rw_product set product_name = ?,product_info = ?,small_img = ?,big_img = ?,type_id = ?,"
					+ "sort = ? where id = ?",productName,productInfo,sqlSmallImg,sqlBigImg,typeId,sort,id);
		}else{
			Db.update("insert into rw_product (product_name,product_info,small_img,big_img,type_id,sort) values ("
					+ "?,?,?,?,?,?)",productName,productInfo,sqlSmallImg,sqlBigImg,typeId,sort);
		}
	}
	
	/**
	 * 删除当前产品信息
	 */
	public void delete(String id){
		Db.deleteById("rw_product", id);
	}
}
